﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class UIVVT : ViewBase
    {
        #region ==VKP===========
        public static readonly DependencyProperty VKPProperty = DependencyProperty.Register("VKP",
            typeof(string), typeof(UIVVT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKPChanged));
        public string VKP
        {
            get
            {
                return (string)GetValue(VKPProperty);
            }
            set
            {
                SetValue(VKPProperty, value);
                LOADBYVKP(value);
            }
        }
        private static void OnVKPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVVT;
            if (src != null)
            {
                src.VKP = (string)e.NewValue;
                
            }
        }
        #endregion       

        #region ==LIEFSCH===========
        public static readonly DependencyProperty LIEFSCHProperty = DependencyProperty.Register("LIEFSCH",
            typeof(string), typeof(UIVVT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLIEFSCHChanged));
        public string LIEFSCH
        {
            get
            {
                return (string)GetValue(LIEFSCHProperty);
            }
            set
            {
                SetValue(LIEFSCHProperty, value);
                OnLIEFSCHChg(value);
            }
        }
        private static void OnLIEFSCHChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVVT;
            if (src != null)
            {
                src.LIEFSCH = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwVVT===========
        public static readonly DependencyProperty dwVVTProperty = DependencyProperty.Register("dwVVT",
            typeof(DataRow), typeof(UIVVT),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwVVTChanged));
        public DataRow dwVVT
        {
            get
            {
                return (DataRow)GetValue(dwVVTProperty);
            }
            set
            {
                SetValue(dwVVTProperty, value);
            }
        }
        private static void OndwVVTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVVT;
            if (src != null)
            {
                src.dwVVT = (DataRow)e.NewValue;
            }
        }
        #endregion


        #region ==IsReadOnly===========
        public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly",
        typeof(bool), typeof(UIVVT),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsReadOnlyChanged));
        public bool IsReadOnly
        {
            get
            {
                return (bool)GetValue(IsReadOnlyProperty);
            }
            set
            {
                SetValue(IsReadOnlyProperty, value);
            }
        }
        private static void OnIsReadOnlyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVVT;
            if (src != null)
            {
                src.IsReadOnly = (bool)e.NewValue;
            }
        }
        #endregion


        #region ==IsWBREQ===========
        public static readonly DependencyProperty IsWBREQProperty = DependencyProperty.Register("IsWBREQ",
            typeof(bool), typeof(UIVVT),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsWBREQChanged));
        public bool IsWBREQ
        {
            get
            {
                return (bool)GetValue(IsWBREQProperty);
            }
            set
            {
                SetValue(IsWBREQProperty, value);
            }
        }
        private static void OnIsWBREQChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVVT;
            if (src != null)
            {
                src.IsWBREQ = (bool)e.NewValue;
                if (!src.IsWBREQ)
                {
                    MessageInfo msgs = new MessageInfo();
                    msgs.Sender = src.RUNID;
                    msgs.MSGTYPE = "WBUNREQ";
                    msgs.ACTION = "WBUNREQ";
                    VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msgs);
                }
                else
                {
                    src.dwVVT = null;
                    src.dwVVT = src.dwMaster;
                }
            }
        }
        #endregion

        #region ==VVT===========
        public static readonly DependencyProperty VVTProperty = DependencyProperty.Register("VVT",
            typeof(string), typeof(UIVVT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVVTChanged));
        public string VVT
        {
            get
            {
                return (string)GetValue(VVTProperty);
            }
            set
            {
                SetValue(VVTProperty, value);
                OnVVTChg(value);
            }
        }
        private static void OnVVTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVVT;
            if (src != null)
            {
                src.VVT = (string)e.NewValue;
            }
        }
        #endregion 

        #region ==PMVKP===========
        public static readonly DependencyProperty PMVKPProperty = DependencyProperty.Register("PMVKP",
            typeof(object), typeof(UIVVT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPMVKPChanged));
        public object PMVKP
        {
            get
            {
                return (object)GetValue(PMVKPProperty);
            }
            set
            {
                SetValue(PMVKPProperty, value);
            }
        }
        private static void OnPMVKPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVVT;
            if (src != null)
            {
                src.PMVKP = (object)e.NewValue;
            }
        }
        #endregion
        public VVVTLIEF TMV
        {
            get
            {
                return (VVVTLIEF)MV;
            }
        }

        public UIVVT()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, System.EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwVVT = dwMaster;
            if (dwMaster == null)
            {
                KEY = ""; 
             }
            else
            { 
                KEY = dwMaster["VVTAUFTYP"].ToString()+ dwMaster["VVTAUFKRS"].ToString() + dwMaster["VVTAUFLFD"].ToString() + dwMaster["VVTAUFPOS"].ToString() + dwMaster["VVTLIEFSCH"].ToString();
                WFORDER= dwMaster["VVTAUFKRS"].ToString() + dwMaster["VVTAUFLFD"].ToString() + dwMaster["VVTAUFPOS"].ToString() + dwMaster["VVTLIEFSCH"].ToString();
            }
        }
        #region====WorkFlow========
        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "WB2KO":
                    VVTLOAD(msg);
                    break; 
                case "VVCHG;":
                    break;
            }
        }
        #endregion

        public void VVTLOAD(MessageInfo msg)
        {
            if (msg == null)
                return;
            SResult = ((VVVTLIEF)MV).VVTLOAD(msg);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
        }

        public virtual void OnVVTChg(string vvt)
        { 
        }
       
        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            base.NewCommand(sender, e);
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            foreach(DataRow row in TBMain.Rows)
            {
                if (row.RowState == DataRowState.Unchanged) continue;
                TMV.UpdateVVTLIEF(row, app.company, 0);
            }
            base.OnSave();
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            
            base.SaveCommand(sender, e);
        }

        public virtual void LOADBYVKP(string vkp)
        {
             
        }

        public virtual void OnLIEFSCHChg(string lief)
        {
             
        }

        
    }
}